package com.dev.base.expand.mvvm;

import com.dev.base.able.BaseModel;
import com.dev.base.able.IBaseViewModel;
import com.dev.base.able.IModelLisenner;
import dev.base.ResourceTable;
import ohos.aafwk.ability.Lifecycle;
import ohos.aafwk.ability.LifecycleObserver;
import ohos.aafwk.ability.LifecycleStateObserver;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.FrameAnimationElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

public abstract class BaseViewModel<BM extends BaseModel> implements LifecycleStateObserver {
    private Context mContext;
    private IModelLisenner iModelLisenner;
    private IBaseViewModel iBaseViewModel;
    private LifecycleObserver lifecycleObserver;
    private CommonDialog popupDialog;
    private static AnimatorProperty animatorProperty;

    public IBaseViewModel getiBaseViewModel() {
        return iBaseViewModel;
    }

    public void setiBaseViewModel(IBaseViewModel iBaseViewModel) {
        this.iBaseViewModel = iBaseViewModel;
    }

    public void setiModelLisenner(IModelLisenner iModelLisenner) {
        this.iModelLisenner = iModelLisenner;
    }

    public void cleaner() {
        getViewModel().cleaner();
    }

    public BaseViewModel(Context mContext) {
        this.mContext = mContext;
    }

    protected abstract BM getViewModel();

    @Override
    public void onStateChanged(Lifecycle.Event event, Intent intent) {

    }

    public void showProgressDialog() {
        if (popupDialog == null) {
            Component component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_layout_loading, null, true);
            Image image=(Image)component.findComponentById(ResourceTable.Id_img);
            image.setScaleMode(Image.ScaleMode.CLIP_CENTER);
            image.setBindStateChangedListener(new Component.BindStateChangedListener() {
                @Override
                public void onComponentBoundToWindow(Component component) {
                    animatorProperty= image.createAnimatorProperty();
                    animatorProperty.rotate(360).setDuration(1000).setLoopedCount(100).start();
                }

                @Override
                public void onComponentUnboundFromWindow(Component component) {

                }
            });

            popupDialog = new CommonDialog(mContext);
            popupDialog.setContentCustomComponent(component);
            popupDialog.setTransparent(true);
            popupDialog.setAlignment(LayoutAlignment.CENTER).setTransparent(true);
            popupDialog.setSize(400, 600);
        }
        popupDialog.show();
    }

    public void dismiss() {
        if(animatorProperty!=null){
            animatorProperty.stop();
            animatorProperty.reset();
        }
        if (popupDialog != null) {
            popupDialog.destroy();
        }
    }
}
